<template>
  <div class="cont">
    <el-tabs v-model="tabActiveName" @tab-click="tabClick">
      <el-tab-pane label="商品分类" :name="1"></el-tab-pane>
      <el-tab-pane label="商品型号" :name="2"></el-tab-pane>
    </el-tabs>
    <!-- 商品分类 -->
    <div class="product" v-if="tabActiveName===1">
      <div class="p-btn">
        <el-button @click="addKinds" size="small">添加分类</el-button>
      </div>
      <div class="p-table">
        <el-table :data="kindsData"> 
          <el-table-column prop="name" label="分类名称" width="180">
            <template #default="scope">
              <!-- {{ scope.row }} -->
              <div v-if="scope.row.level==1" class="bg-gray">{{scope.row.name}}</div>
              <div v-if="scope.row.level==2" class="bg-left">{{scope.row.name}}</div>
            </template>
          </el-table-column>
          <el-table-column label="图标显示" width="180"> 
            <template #default="scope">
              <el-switch  :active-value="1" :inactive-value="0" v-model="scope.row.is_channel" @change="changeChannelStatus($event,scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="首页显示" width="180"> 
            <template #default="scope">
              <el-switch
                  active-text=""
                  inactive-text=""
                  v-model="scope.row.is_show"
                  :active-value="1" :inactive-value="0"
                  @change='changeShowStatus($event,scope.row)'>
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="全部产品页面显示" width="140">
              <template #default="scope">
                  <el-switch
                      active-text=""
                      inactive-text=""
                      v-model="scope.row.is_category"
                      :active-value="1" :inactive-value="0"
                      @change='changeCategoryStatus($event,scope.row)'>
                  </el-switch>
              </template>
          </el-table-column>

          <el-table-column prop="sort_order" label="排序" width="100" sortable>
              <template #default="scope">
                  <el-input v-model="scope.row.sort_order" placeholder="排序" @change="submitSort(scope.row)"></el-input>
              </template>
          </el-table-column>

          <el-table-column label="操作" width="300">
              <template #default="scope">
                  <el-button size="small" @click="editCategory(scope.$index, scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteCategory(scope.row)">删除
                  </el-button>
              </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 商品型号 -->
    <div class="product" v-if="tabActiveName===2">
      <div class="p-btn">
        <el-button @click="addSpec" size="small">添加型号</el-button>
      </div>
      
      <el-table  :data="specData" style="width: 100%" border stripe> 
        <el-table-column prop="id" label="ID">
        </el-table-column>
        <el-table-column prop="name" label="型号名">
        </el-table-column>
        <el-table-column prop="sort_order" label="排序">
        </el-table-column>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="specEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="small" type="danger" @click="specDelete(scope.$index, scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { getCategoryList, updateCategory, deleteCategory, getSpecList,deleteSpecById} from '@/api/product'
export default {
  name:'商品设置',
  data(){
    return {
      tabActiveName:1,
      kindsData:[],
      specData:[]
    }
  },
  mounted(){
    this.initCategory() 
  },
  methods:{
    async initCategory(){
      let res = await getCategoryList()
      if(res.code === 0){
        this.kindsData = res.data
      }
    },
    async initSpec(){
      console.log('initSpec')
      let res = await getSpecList()
      if(res.code === 0){
        this.specData = res.data
      }
    },
    tabClick(tab,event){
      if(this.tabActiveName === 1){
        // 商品分类
        this.initCategory()
      }else if(this.tabActiveName === 2){
        // 型号
        this.initSpec()
        console.log('initSpec')
      }
    },
    addKinds(){
      this.$router.push({path:'/product/setting/category'})
    },
    async changeChannelStatus(value,row){
      console.log('changeChannelStatus value:',value)
      console.log('changeChannelStatus row:',row)
      let res = await updateCategory({ id:row.id, is_channel:value })
      if(res.code === 0){
        this.$message({
          type:'success',
          message:'图标是否显示更新成功'
        })
      } else {
        this.$message({
          type:'error',
          message:'图标是否显示更新失败'
        })
      }
    },
    async changeShowStatus(value,row){
      // 修改首页是否显示
      let res = await updateCategory({ id:row.id, is_show:value })
      if(res.code === 0){
        this.$message({
          type:'success',
          message:'是否首页显示更新成功'
        })
      } else {
        this.$message({
          type:'error',
          message:'是否首页显示更新失败'
        })
      } 
    },
    async changeCategoryStatus(value,row){
      // 修改全部产品页是否显示
      let res = await updateCategory({ id:row.id, is_category:value })
      if(res.code === 0){
        this.$message({
          type:'success',
          message:'是否产品页显示更新成功'
        })
      } else {
        this.$message({
          type:'error',
          message:'是否产品页显示更新失败'
        })
      } 
    },
    async submitSort(row){
      // 修改排序
      // console.log('submitSort row:',row)
      let res = await updateCategory({ id:row.id, sort_order:+row.sort_order })
      if(res.code === 0){
        this.$message({
          type:'success',
          message:'排序更新成功'
        })
      } else {
        this.$message({
          type:'error',
          message:'排序更新失败'
        })
      } 
    },
    async deleteCategory(row){
      // 删除分类
      this.$confirm('确定要删除这条记录吗').then(async res=>{
        if(res==='confirm'){
          let res = await deleteCategory({ id : row.id })
          if(res.code === 0){
            this.$message({
              type:'success',
              message:'删除分类成功'
            })
          }else{
            this.$message({
              type:'error',
              message:'删除分类失败'
            })
          }
          this.initCategory()
        }
      }).catch(err=>{
        console.log('err:',err)
      })
      
      // console.log('res:',res)
    },
    editCategory(index,row){
      // 编辑分类
      // console.log('index:',index)
      // console.log('row:',row)
      this.$router.push({path:'/product/setting/category',query:{id:row.id}})
    },
    addSpec(){
      // 添加商品型号
      this.$router.push({path:'/product/setting/spec'})
    },
    specEdit(index,row){
      // 编辑商品型号
      this.$router.push({path:'/product/setting/spec',query:{id:row.id}})
    },
    async specDelete(index,row){
      // 删除商品型号
      this.$confirm('确定要删除这条记录吗').then(async res=>{
        if(res === 'confirm'){
          let res = await deleteSpecById({id:row.id})
          if(res.code === 0){
            this.$message({
              type:'success',
              message:'删除型号成功'
            })
            this.initSpec()
          }else{
            this.$message({
              type:'error',
              message:'删除失败'
            })
          }
        }
        
      }).catch(err=>{
        console.log('err:',err)
      })
      
    }
  }
}
</script>

<style lang="scss" scoped>
.cont{
  .product{
    .p-btn{
      text-align: left;
      margin-bottom: 10px;
    }
  }
}
</style>


